package com.superteam.zzl.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

/**
 * Created by Administrator on 2017/1/6.
 */

public class PullToRefreshRecyclerView extends PullToRefreshBase<RecyclerView> {

    private RecyclerView mRefreshableView;

    public PullToRefreshRecyclerView(Context context) {
        super(context);
    }

    public PullToRefreshRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public Orientation getPullToRefreshScrollDirection() {
        return Orientation.VERTICAL;
    }

    @Override
    protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
        mRefreshableView = new RecyclerView(context, attrs);
        return mRefreshableView;
    }

    @Override
    protected boolean isReadyForPullEnd() {
        return isLastItemVisible();
    }

    private boolean isFirstItemVisible() {
        RecyclerView.Adapter adapter = mRefreshableView.getAdapter();
        if (null == adapter || adapter.getItemCount() == 0) {
//            Log.e("PullToRefreshRecyclerView", "isFirstItemVisible. Empty View");
            return true;
        } else {
            // 第一个条目完全展示,可以刷新
            if (getFirstVisiblePosition() == 0) {
                int top = mRefreshableView.getChildAt(0).getTop();
                int top1 = mRefreshableView.getTop();
//                Log.e("PullToRefreshRecyclerView", "isFirstItemVisible: top : " + top + "top" + top1);
                return top >= top1;
            }
        }
        return false;
    }

    private int getFirstVisiblePosition() {
        View firstVisibleChild = mRefreshableView.getChildAt(0);
//        Log.e("PullToRefreshRecyclerView", "getFirstVisiblePosition: " + mRefreshableView.getChildAt(3).getTop());
        int childAdapterPosition = mRefreshableView.getChildAdapterPosition(firstVisibleChild);
//        Log.e("PullToRefreshRecyclerView", "getFirstVisiblePosition: " + childAdapterPosition);
        return firstVisibleChild != null ? childAdapterPosition : -1;

    }

    @Override
    protected boolean isReadyForPullStart() {
        return isFirstItemVisible();
    }

    private boolean isLastItemVisible() {
        RecyclerView.Adapter adapter = mRefreshableView.getAdapter();
        int lastItemPosition = adapter.getItemCount() - 1;
        int lastVisiblePosition = getLastVisiblePosition();
//        View childAt = mRefreshableView.getChildAt(mRefreshableView.getChildCount() - 1);
//        Log.e("PullToRefreshRecyclerView", lastVisiblePosition + "isLastItemVisible: " + lastItemPosition + "childAt" + childAt.getBottom());
       /* if (childAt != null) {
            Log.e("PullToRefreshRecyclerView", "isLastItemVisible: " + lastVisiblePosition +
                    "bottom" + childAt.getBottom() + "bottom" + mRefreshableView.getBottom());
        } else {
            Log.e("PullToRefreshRecyclerView", "isLastItemVisible: nulllll");
        }*/
        if (lastVisiblePosition == lastItemPosition) {
            // 最后一个条目完全展示,可以刷新
            int bottom = mRefreshableView.getChildAt(mRefreshableView.getChildCount() - 1).getBottom();
            int bottom1 = mRefreshableView.getBottom();
//            Log.e("PullToRefreshRecyclerView", "isLastItemVisible: " + lastItemPosition + "bottom" + bottom + "bottom" + bottom1);
            return bottom <= bottom1;
        }
        return false;
    }

    private int getLastVisiblePosition() {
        View lastVisibleChild = mRefreshableView.getChildAt(mRefreshableView.getChildCount() - 1);
        int childAdapterPosition = mRefreshableView.getChildAdapterPosition(lastVisibleChild);
//        Log.e("PullToRefreshRecyclerView", "getLastVisiblePosition: " + childAdapterPosition);
        return lastVisibleChild != null ? childAdapterPosition : -1;
    }

    public void setAdapter(RecyclerView.Adapter adapter)
    {
        mRefreshableView.setAdapter(adapter);
    }

    public void setLayoutManager(RecyclerView.LayoutManager layout) {
        mRefreshableView.setLayoutManager(layout);
    }

    public void addItemDecoration(RecyclerView.ItemDecoration decor) {
        mRefreshableView.addItemDecoration(decor);
    }

    public void getAdapter() {
        mRefreshableView.getAdapter();
    }

    public void setItemAnimator(RecyclerView.ItemAnimator animator) {
        mRefreshableView.setItemAnimator(animator);
    }

}
